package dao.impl;

import dao.IntegralDao;
import domian.Integral;
import utils.BaseDAO;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

@SuppressWarnings("all")
public class IntegralDaoImpl implements IntegralDao {

    @Override
    public int addIntegral(Integral integral) {
        String sql = "insert into integral_change (uid,fid,file_title,integral,cause)values(?,?,?,?,?)";

        Object[] obj = {integral.getUid(), integral.getFid(), integral.getFile_title(), integral.getIntegral(), integral.getCause()};

        int i = BaseDAO.executeUpdate(sql, obj);

        return i;
    }

    //查询单个
    @Override
    public Integral inquire(Integer id) {
        Integral integra = null;
        Connection connection = null;
        PreparedStatement ps = null;
        ResultSet resultSet = null;
        try {
            //获取连接
            connection = BaseDAO.getConnection();
            //创建SQL语句
            String sql = "select * from integral_change where id=?";
            //获取SQL语句对象
            ps = connection.prepareStatement(sql);
            ps.setInt(1, id);
            //执行SQL语句
            resultSet = ps.executeQuery();
            if (resultSet.next()) {
                Integer uid = resultSet.getInt("uid");
                Integer fid = resultSet.getInt("fid");
                String file_title = resultSet.getString("file_title");
                Integer integral = resultSet.getInt("integral");
                String cause = resultSet.getString("cause");
                String causeTime = resultSet.getString("causeTime");
                integra = new Integral(id, uid, fid, file_title, integral, cause, causeTime);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            //关闭资源
            BaseDAO.close(connection, ps, resultSet);
        }
        return integra;
    }

    //根据uid查询积分明细+分页
    @Override
    public List<Integral> inquire(Integer uid, Integer which, Integer page) {
        List<Integral> integrals = new ArrayList<>();
        Connection connection = null;
        PreparedStatement ps = null;
        ResultSet resultSet = null;
        try {
            //获取连接
            connection = BaseDAO.getConnection();
            //创建SQL语句
            String sql = "select * from integral_change where uid=? ORDER BY id DESC";
            if (which != null && page != null) {
                sql += " limit ?,?";
            }
            //获取SQL语句对象
            ps = connection.prepareStatement(sql);
            ps.setInt(1, uid);
            if (which != null && page != null) {
                ps.setInt(2, which);
                ps.setInt(3, page);
            }
            //执行SQL语句
            resultSet = ps.executeQuery();
            while (resultSet.next()) {
                Integer id = resultSet.getInt("id");
                Integer fid = resultSet.getInt("fid");
                String file_title = resultSet.getString("file_title");
                Integer integral = resultSet.getInt("integral");
                String cause = resultSet.getString("cause");
                String causeTime = resultSet.getString("causeTime");
                integrals.add(new Integral(id, uid, fid, file_title, integral, cause, causeTime));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            //关闭资源
            BaseDAO.close(connection, ps, resultSet);
        }
        return integrals;
    }

    @Override
    public List<Integral> findInquireAll(Integer ep) {
        List<Integral> list = new ArrayList<>();

        Connection connection = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            //获取连接
            connection = BaseDAO.getConnection();
            //创建SQL语句
            String sql = "select * from integral_change where uid=?";
            //获取SQL语句对象
            ps = connection.prepareStatement(sql);
            ps.setInt(1, ep);
            //执行SQL语句
            rs = ps.executeQuery();

            while (rs.next()) {
                Integer id = rs.getInt("id");
                Integer uid = rs.getInt("uid");
                Integer fid = rs.getInt("fid");
                String file_title = rs.getString("file_title");
                Integer integral = rs.getInt("integral");
                String cause = rs.getString("cause");
                String causeTime = rs.getString("causeTime");
                Integral integra = new Integral(id, uid, fid, file_title, integral, cause, causeTime);
                list.add(integra);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            //关闭资源
            BaseDAO.close(connection, ps, rs);
        }

        return list;
    }
}
